package com.rivulet.doj_backend.service.impl;

import com.rivulet.doj_backend.mapper.QuestionMapper;
import com.rivulet.doj_backend.pojo.Question;
import com.rivulet.doj_backend.pojo.UserInformation;
import com.rivulet.doj_backend.service.QuestionService;
import com.rivulet.doj_backend.tools.ResultFul;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.rivulet.doj_backend.tools.ResultFul;

import java.util.ArrayList;
import java.util.List;

@Service
public class QuestionServiceImpl implements QuestionService {
    @Autowired
    QuestionMapper questionMapper;

    @Override
    public ResultFul inquiryQuestion(Integer question_id) {
        ResultFul result;
        try {
            Question realquestion = questionMapper.getQuestionByQuestionId(question_id);
            if (realquestion != null) {
                List list = new ArrayList();
                list.add(realquestion);
                result = new ResultFul<List>(100, list);
            } else {
                result = new ResultFul<Object>(407, null);
            }
        } catch (Exception e) {
            System.out.println("inqiryQuestion_error:" + e);
            result = new ResultFul<Object>(404, null);
        }

        return result;




    }

    @Override
    public ResultFul showQuestion(Integer page) {
        ResultFul result;
        try {
            if (page == null){
                page = 1;
                Question realquestion = questionMapper.showQuestion(page);
                List list = new ArrayList();
                list.add(realquestion);
                result = new ResultFul<List>(100, list);
            }else {
                Question realquestion = questionMapper.showQuestion(page);
                List list = new ArrayList();
                list.add(realquestion);
                result = new ResultFul<List>(100, list);
            }
        } catch (Exception e){
            System.out.println("showQuestion_error:"+e);
            result = new ResultFul<List>(404, null);
        }
        return result;
    }


    @Override
    public ResultFul addQuestion(Integer user_id,String question_title,String question_content,Integer diffculty,Integer label_id,Integer question_id){
        ResultFul result;
        try {
            if (user_id != null&& question_id !=null){
                questionMapper.addLabelId(user_id,label_id);
                questionMapper.addQuestion(question_id, question_title, question_content, diffculty);
                questionMapper.addUserCollectQuestion(user_id, question_id);
                result = new ResultFul<List>(100, null);
            }else {
                result = new ResultFul<List>(408, null);
            }
        } catch (Exception e){
            System.out.println("addQuestion_error:"+e);
            result = new ResultFul<List>(404, null);
        }
        return result;
    }

}
